/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.model.actor.instance;

import java.util.StringTokenizer;

import com.l2jserver.gameserver.events.LMSEvent;
import com.l2jserver.gameserver.faction.MapManager;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.entity.TvTEvent;
import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.network.serverpackets.RadarControl;
import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
import com.l2jserver.util.Rnd;

/**
 * @author Laikeriz
 */
public final class L2FTeleInstance extends L2Npc
{
	public L2FTeleInstance(int objectId, L2NpcTemplate template)
	{
		super(objectId, template);
	}
	
	@Override
	public void onBypassFeedback(L2PcInstance player, String command)
	{
		player.sendPacket(ActionFailed.STATIC_PACKET);
		StringTokenizer st = new StringTokenizer(command, " ");
		String actualCommand = st.nextToken();
		if (actualCommand.startsWith("teleToFlag"))
		{
			//			int npcId = getNpcId();
			if (st.countTokens() <= 0)
				return;
			int flagId = Integer.parseInt(st.nextToken());
			L2FactionCrystalInstance currentFlag = MapManager.getCurrentMap().getSpawnedFlag(flagId);
			if (!checkCondition(player, currentFlag))
				return;
			player.teleToLocation(currentFlag.getX() + Rnd.get(20), currentFlag.getY() + Rnd.get(20), currentFlag.getZ() + 50);
		}
		else if (actualCommand.startsWith("lmsreg"))
		{
			if (LMSEvent.handleRegistrant(player, true))
				player.sendMessage("You have successfully registered to LMS event");
			else
				player.sendMessage("You are already registered or registration is not in effect");
		}
		else if (actualCommand.startsWith("showArrow"))
		{
			if (st.countTokens() <= 0)
				return;
			int flagId = Integer.parseInt(st.nextToken());
			L2FactionCrystalInstance currentFlag = MapManager.getCurrentMap().getSpawnedFlag(flagId);
			player.sendPacket(new RadarControl(0, 1, currentFlag.getX(), currentFlag.getY(), currentFlag.getZ()));
		}
		
		//		super.onBypassFeedback(player, command);
	}
	
	@Override
	public void showChatWindow(L2PcInstance player, int val)
	{
		int npcId = getTemplate().npcId;
		NpcHtmlMessage html = new NpcHtmlMessage(getObjectId());
		StringBuilder buttons = new StringBuilder();
		for (L2FactionCrystalInstance flag : MapManager.getCurrentMap().getSpawnedFlags().values())
			if (flag.getFaction() == player.getFaction())
				buttons.append("<button value=\"" + flag.getName() + "\" action=\"bypass -h npc_%objectId%_teleToFlag " + flag.crystalId + "\" width=233 height=22 back=\"L2UI_CH3.bigbutton6_over\" fore=\"L2UI_CH3.bigbutton6\">");
		for (L2FactionCrystalInstance flag : MapManager.getCurrentMap().getSpawnedFlags().values())
			if (flag.getFaction() != player.getFaction())
				buttons.append("<button value=\"Location - " + flag.getName() + " \" action=\"bypass npc_%objectId%_showArrow " + flag.crystalId + "\" width=233 height=22 back=\"L2UI_CH3.bigbutton6\" fore=\"L2UI_CH3.bigbutton6_down\">");
		html.setFile(getHtmlPath(npcId, val));
		html.replace("%map%", MapManager.getCurrentMap().getName());
		html.replace("%buttons%", buttons.toString());
		html.replace("%objectId%", String.valueOf(getObjectId()));
		player.sendPacket(html);
		player.sendPacket(ActionFailed.STATIC_PACKET);
	}
	
	public static boolean checkCondition(L2PcInstance player, L2FactionCrystalInstance flag)
	{
		String message = "";
		boolean cond = true;
		if (flag == null)
			return false;
		
		if (player.getFaction() != flag.getFaction())
		{
			message = "Flag has been overtaken by another faction";
			cond = false;
		}
		if (flag.promile < flag.forbidTp * 10)
		{
			message = "Flag is less than " + flag.forbidTp * 10 + " owned by your faction";
			cond = false;
		}
		if (player.isInOlympiadMode() || (TvTEvent.isStarted() && TvTEvent.isPlayerParticipant(player.getObjectId())))
		{
			message = "Cannot teleport to flag due to inappropriate conditions";
			cond = false;
		}
		if (message != "")
			player.sendMessage(message);
		return cond;
	}
	
	@Override
	public String getHtmlPath(int npcId, int val)
	{
		return "data/html/faction/teleport" + (val == 0 ? "" : String.valueOf(val)) + ".htm";
	}
}
